import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from "@angular/core";
import {CommService} from "../comm-service";
import {SharedService} from "../shared-service";
import * as e from "echarts";
import {LocalStorageService} from "../local-storage-service";
import {MyhttpService} from "../myhttp.service";

@Component({
  template: `
    <div class="sjbxx comp" [style.width]="compw" [style.height]="comph" [style.top]="topv"
         [style.left]="leftv">
      <div class="content">
        <div class="title">
          <div class="title_text">试卷包详情 {{comptitle}}</div>
        </div>
        <div class="body">
          <div class="ctitle">
            <!--<div class="bqsjbmc">{{bqsjbxx.mc}}</div>-->
            <div class="bqsjbzs">试卷袋总数：&nbsp;&nbsp;{{bqsjbxx.zs}}</div>
          </div>
          <div class="kmRoom iecrollkf chromescrollkf">
            <table class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>科目</th>
                <th>正卷袋数量</th>
                <th>备用卷袋数量</th>
              </tr>
              </thead>
              <tbody *ngIf="bqsjbxx.kmlist.length">
              <tr *ngFor="let row of bqsjbxx.kmlist">
                <td>{{row.kmmc}}</td>
                <td>{{row.zjdsl}}</td>
                <td>{{row.byjsl}}</td>
              </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  `,
  styles: [
    ".sjbxx{position:absolute;background-color:#fff;border:5px solid #ccc;}",
    ".content{width: calc(100%);height: calc(100%);}",
    ".content>.title{position: relative;width: 100%;height: auto;padding: 10px; border-bottom: 1px solid #ccc;color: rgb(138 143 151);}",
    ".title>.title_text{height: 15px;line-height: 15px;font-size: 15px;  width: auto;border-left: 5px solid #1477F3;padding-left: 10px}",
    ".content>.body{position: relative;width: 100%;height: calc(100% - 24px);padding: 10px 10px}",
    ".ctitle{height: 30px;line-height: 30px;color: #0E97E6;padding: 0px 15px}",
    // ".bqsjbmc{display: inline-block;width: 50%;}",
    ".bqsjbzs{display: inline-block;width: 100%;text-align: right}",
    ".kmRoom{width: calc(100% - 17px);margin-top: 10px;height:243px;overflow-y: auto }"
  ]
})
export class BmskdSjbxxComponent implements OnInit, OnDestroy {
  compw: string = "0px";
  comph: string = "0px";
  topv: string = "0px";
  leftv: string = "0px";
  comptitle: string = "";
  frompage: string;
  s_obj: any = {
    ksjhbh: "",
    dzbqidList: []
  };
  bqsjbxx: any = {
    mc: "",
    zs: 0,
    kmlist: []
  };


  getdata(): void {
    if (!this.s_obj.ksjhbh || !this.s_obj.dzbqidList.length) {
      this.bqsjbxx.zs = 0;
      this.bqsjbxx.kmlist.length = 0;
      return
    }
    const url = "sjgz/gjcx/cxbqglsjxx";
    this.itservice.kwPtPost(url, this.s_obj).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const dzbqGlSjxxListv = resp.dzbqGlSjxxList ? resp.dzbqGlSjxxList : [];
            if (!dzbqGlSjxxListv.length) {
              this.bqsjbxx.zs = 0;
              this.bqsjbxx.kmlist.length = 0;
              return
            }
            const activebq_glSjxxList = dzbqGlSjxxListv[0].glSjxxList;
            let sjdzs: number = 0; // 试卷袋总数
            this.bqsjbxx.kmlist = activebq_glSjxxList.map((kmobj) => {
              const zjsjdslv = kmobj.zjsjdsl; // 正卷试卷袋数量
              const zjdtkdslv = kmobj.zjdtkdsl; // 正卷答题卡袋数量
              const byjdslv = kmobj.byjdsl; // 备用卷袋数量
              const byjdtkdslv = kmobj.byjdtkdsl; // 备用卷答题卡数量
              sjdzs += zjsjdslv;
              sjdzs += zjdtkdslv;
              sjdzs += byjdslv;
              sjdzs += byjdtkdslv;
              return {
                kmmc: kmobj.kmmc,
                zjdsl: zjsjdslv + zjdtkdslv,
                byjsl: byjdslv + byjdtkdslv
              }
            });
            this.bqsjbxx.zs = sjdzs;
          }
        }
      });
    });
  }

  /**
   * 组件在 显示面板中的位置+ 大小
   */
  initcompPositionSize_ctrshow(): void {
    // 位置 大小
    this.compw = this.o.compw + "px";
    this.comph = this.o.comph + "px";
    this.topv = this.o.topv + "px";
    this.leftv = this.o.leftv + "px";
    this.s_obj.ksjhbh = this.localStorageService.get("hksjh");
    // 显示
    this.frompage = this.o.frompage;
    if (this.frompage === "bmskd") {

    }
  }


  @Input() o: any;
  @Output() outhander: EventEmitter<any> = new EventEmitter();

  constructor(private commservice: CommService,
              private localStorageService: LocalStorageService,
              private itservice: MyhttpService,
              private sharedService: SharedService) {
    this.sharedService.changeEmitted$.subscribe(resp => {
      const actiontype = resp.actiontype;
      const datav = resp.data;
      switch (actiontype) {
        case "toggleActiveBq": {
          const activebqobj = datav.activebqobj;
          this.comptitle = `(${activebqobj.text})`;
          this.bqsjbxx.mc = activebqobj.text;
          this.s_obj.dzbqidList = [activebqobj.id];
          this.getdata();
        }
          break;
      }
    });
  }

  ngOnInit() {
    this.commservice.log("-- 【 compkey=sjbxx】--被父组件动态加载init....", this.o);
    this.initcompPositionSize_ctrshow();
  }

  ngOnDestroy() {

  }

}
